package com.example.menulhd;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private LeftFragment leftFragment;
    private TextView tv_recommend,tv_must_buy;
    private RightFragment rightFragment;
    private String[] names1 ={"爆款*肥牛鱼豆腐骨肉相连三荤一素一份米饭","豪华双人餐","【热销】双人套餐（含两份米饭）"};
    private String[] sales1 ={"月售422 好评80%","月售242 好评率78%","月售234 好评率77%"};
    private String[] prices1 ={"$23" ,"$41","$32"};
    private int[] imgs1 ={R.drawable.recom_one,R.drawable.recom_two,R.drawable.recom_three};

    private String[] names2 ={"蔬菜一人套餐","豪华双人餐","三人套餐"};
    private String[] sales2 ={"月售4245 好评80%","月售242 好评率78%","月售234 好评率77%"};
    private String[] prices2 ={"$23" ,"$566","$3535"};
    private int[] imgs2 ={R.drawable.must_buy_one,R.drawable.must_buy_two,R.drawable.must_buy_three};

    private Map<String, List<FoodBean>> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setDate();
        init();
        clickEvent();
    }
    private void init (){
        fragmentManager =getSupportFragmentManager();
        leftFragment =(LeftFragment) fragmentManager.findFragmentById(R.id.left);
        tv_recommend =leftFragment.getView().findViewById(R.id.tv_recommend);
        tv_must_buy =leftFragment.getView().findViewById(R.id.tv_must_buy);
    }
    private void setDate(){
        map=new HashMap<>();
        List<FoodBean> list1 =new ArrayList<>();
        List<FoodBean> list2 =new ArrayList<>();
        for(int i =0; i<names1.length; i++){
            FoodBean bean =new FoodBean();
            bean.setName(names1[i]);
            bean.setSales(sales1[i]);
            bean.setPrice(prices1[i]);
            bean.setImg(imgs1[i]);
            list1.add(bean);
        }
        map.put("1",list1);
        for(int i =0; i<names2.length; i++){
            FoodBean bean =new FoodBean();
            bean.setName(names2[i]);
            bean.setSales(sales2[i]);
            bean.setPrice(prices2[i]);
            bean.setImg(imgs2[i]);
            list2.add(bean);
        }
        map.put("2",list2);
    }
    private void clickEvent(){
        tv_recommend.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                switchData(map.get("1"));
                tv_recommend.setBackgroundColor(Color.WHITE);
                tv_must_buy.setBackgroundResource(R.color.gray);
            }
        });

        tv_must_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchData(map.get("2"));
                tv_must_buy.setBackgroundColor(Color.WHITE);
                tv_recommend.setBackgroundResource(R.color.gray);
            }
        });  switchData(map.get("1"));
    }

    public void switchData(List<FoodBean> list){
        fragmentManager =getSupportFragmentManager();
        fragmentTransaction =fragmentManager.beginTransaction() ;
        rightFragment =new RightFragment().getInstance(list);
        fragmentTransaction.replace(R.id.right,rightFragment);
        fragmentTransaction.commit();
    }
}